package com.juku2024.juku.adapter

import com.chad.library.adapter.base.BaseDelegateMultiAdapter
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.juku2024.juku.R
import com.juku2024.juku.tools.Constants

class LocalEpisodeAdapter(val episodes: ArrayList<String>?) :
    BaseDelegateMultiAdapter<String, BaseViewHolder>(episodes) {
    var defaultPos: Int = 0
    // 是否有解锁广告
    var isLockAd = false
    var lockEps: Int = 0
    var category: Int = Constants.VIDEO_CATEGORY_ALBUM

    init {
        setMultiTypeDelegate(object : BaseMultiTypeDelegate<String>() {
            override fun getItemType(data: List<String>, position: Int): Int {
                return  if (category == Constants.VIDEO_CATEGORY_VARIFY || category == Constants.VIDEO_CATEGORY_MOVIE) {
                    1
                } else {
                    0
                }
            }
        })

        getMultiTypeDelegate()?.apply {
            addItemType(0, R.layout.item_episode_small)
            addItemType(1, R.layout.item_episode_large)
        }
    }

    override fun convert(holder: BaseViewHolder, item: String) {
        val isPlaying = defaultPos == holder.layoutPosition
        val bgRes = if (isPlaying) {
            R.drawable.shape_controller_episode_select
        } else {
            R.drawable.shape_controller_episode
        }
        holder.setText(R.id.tv_episode, item)
            .setBackgroundResource(R.id.tv_episode, bgRes)

        // 1.有解锁广告; 2.需要解锁集数 > 0; 3.当前位置处在最后需要解锁集数范围内
        val length =  episodes?.size?:0
        val isVisibility = if (isLockAd && lockEps > 0) { // 有解锁广告且设置了解锁集数
            if (category == Constants.VIDEO_CATEGORY_VARIFY) { // 综艺倒序
                holder.layoutPosition < lockEps
            } else {
                length - holder.layoutPosition <= lockEps
            }
        } else {
            false
        }
        holder.setVisible(R.id.epi_top_icon, isVisibility)
    }
}